/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jun 1, 2012  11:30:13 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.construction.payment.request
 * File Name : RequestPaymentLowRiseProcessing.java
 * TODO
 */
package apps.business.process.construction.payment.request;

import java.util.List;

import jpa.entitys.promis.construction.VwSyncContract;
import jpa.entitys.promis.construction.VwSyncRequestPayment;
import jpa.entitys.synchro.staging.MmLivcontractorDeduct;
import jpa.entitys.synchro.staging.MmLivcontractorOutb;
import jpa.service.synchro.staging.MmLivcontractorOutbServices;

import org.apache.log4j.Logger;

import promis.utility.CommonService;

import sap.business.mm.outbound.VwSyncRequestPaymentService;

import apps.business.process.SyncProcessorCommons;
import apps.promis.out.construction.VwSyncPoContractorAction;
import apps.promis.out.construction.VwSyncRequestPaymentAction;

/**
 * @author chatree
 *
 */
public class RequestPaymentLowRiseProcessing extends SyncProcessorCommons {
	
	private static final Logger log = Logger.getLogger(RequestPaymentLowRiseProcessing.class);	
	
	/**
	 * 		String company = "SP";
			String project = "SPLVILLE-PRAGSA";
			String documentNo = "PB-1206-000002";
			String status = "A";
			String syncIsstaging = "Y";
			String refType = "CT";
	 * @param company
	 * @param project
	 * @param documentNo
	 * @param refType
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 */
	public void Processing(String company, String project, String documentNo, String refType, String status, String syncIsstaging) throws Exception {		
		try{
			
			System.out.println("Find Request for Payment low rise ["+ company +" || " + project +" || "+ documentNo +" || "+ refType +" || " + status +"] in PROMIS --->");
			UPD_PROMISUseTime(true, false, null);
			List<Object> value_list = new VwSyncRequestPaymentAction().CreateToStaging(company, project, documentNo, refType, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);
			
			if(CommonService.getSize(value_list) > 0){
				List<MmLivcontractorOutb> mmliv = null;
				List<MmLivcontractorDeduct> mmlivDeduct = null;
				
				if(CommonService.getSize(value_list) == 1){
					mmliv = (List<MmLivcontractorOutb>)value_list.get(0);
				}else if(CommonService.getSize(value_list) == 2){
					mmliv = (List<MmLivcontractorOutb>)value_list.get(0);
					mmlivDeduct = (List<MmLivcontractorDeduct>)value_list.get(1);	
				}
				
				UPD_SENDUseTime(true, false, mmliv.get(0).getInstanceNo());
				new VwSyncRequestPaymentService().SendToSAP(mmliv, mmlivDeduct);
				UPD_SENDUseTime(false, true, mmliv.get(0).getInstanceNo());
			}
			System.out.println("Sending ["+ company +" || " + project +" || "+ documentNo +" || "+ refType +" || " + status +"] to SAP size=" + CommonService.getSize(value_list));
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	private void ReRunAll() throws Exception {
		try{
			
			String company ="SP";
			String project = "VSP";

			List<VwSyncRequestPayment> lst = new VwSyncRequestPaymentAction().ReRunAll(company, project);
			if(!CommonService.IsNull(lst)){
				
				for(VwSyncRequestPayment value : lst)				
					Processing(value.getCompany(), value.getProject(), value.getRequestNo(), value.getRefType(), value.getStatus(), value.getSyncIsstaging());
			}
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	@Deprecated
	private void Start() throws Exception {
		
		try{

			String company = "SP";
			String project = "VRAM";
			String documentNo = "PW-1302-000003";
			String status = "A";
			String syncIsstaging = "Y";
			String refType = "CT";
			
			List<Object> value_list = new VwSyncRequestPaymentAction().CreateToStaging(company, project, documentNo, refType, status, syncIsstaging);			
			System.out.println("Create to Staging  value_list : " + CommonService.getSize(value_list));
			if(CommonService.getSize(value_list) > 0){
				List<MmLivcontractorOutb> mmliv = null;
				List<MmLivcontractorDeduct> mmlivDeduct = null;
				
				if(CommonService.getSize(value_list) == 1){
					mmliv = (List<MmLivcontractorOutb>)value_list.get(0);
				}else if(CommonService.getSize(value_list) == 2){
					mmliv = (List<MmLivcontractorOutb>)value_list.get(0);
					mmlivDeduct = (List<MmLivcontractorDeduct>)value_list.get(1);	
				}
				
				System.err.println("mmliv=" + CommonService.getSize(mmliv));
				System.err.println("mmlivDeduct=" + CommonService.getSize(mmlivDeduct));
				
				new VwSyncRequestPaymentService().SendToSAP(mmliv, mmlivDeduct);
				System.out.println("Send to SAP Size :" + CommonService.getSize(value_list));
			}
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			
			new RequestPaymentLowRiseProcessing().Start();
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			log.error(e);
		}
	}

}
